<?php
/**
 * 
 * @author tangbl
 *
 */
class ThinkForm {
	
	private $_lang;
	private $_form; 
	 
	/**
	 * 构造方法，用于实例化表单类
	 * @param string $
	 */
	public function __construct($form){
		$this->_form = $form;	
		$this->_lang = getLang();
	}
	
	/**
	* 创建附属模型表单元素
	*/
	public function createAttached(){
		$html = '';
		$html .= $this->_linkJS();
				
		$html .= $this->createFormAll($this->_form);
		
		$html .= $this->_validate();
		$html .= '<script type="text/javascript">';
		$html .= 'jQuery(document).ready(function() {';
		$html .= $this->_form['module'].'From.init();';
		$html .= '});';
		$html .= '</script>';
		
		return $html;
	}
	
	
	/**
	 * 创造单个表单
	 */
	public function createForm(){
		$html = '';
		$html = $this->_linkJS();
		$html .= '<form action="'.$this->_form['action'].'" method="post" id="'.$this->_form['module'].'From" class="form-horizontal form-bordered form-row-stripped">';
		foreach($this->_form['hidden'] as $k=>$v){
			$html .= '<input type="hidden" name="'.$k.'" value="'.$v.'"/>';
		}
		$html .= '<div class="alert alert-error hide">';
		$html .= '<button class="close" data-dismiss="alert"></button>';
		$html .= L('cfg_form_err_allform');
		$html .= '</div>';
		$html .= '<div class="alert alert-success hide">';
		$html .= '<button class="close" data-dismiss="alert"></button>';
		$html .= L('cfg_form_ok_allform');
		$html .= '</div>';
		$html .= '<div class="tabbable tabbable-custom">';
		
		if(is_array($this->_form['tab'])){
			$html .= $this->createFormTab($this->_form);
		}else{
			$html .= $this->createFormAll($this->_form);
		}
		
		$html .= '</div>';
		$html .= '<div class="form-actions"><button type="submit" class="btn purple">'.L('cfg_form_ele_submit').'</button> <button type="reset" class="btn">'.L('cfg_form_ele_submit').'</button></div>';
		$html .= '</form>';
		$html .= $this->_validate();
		$html .= '<script type="text/javascript">';
		$html .= 'jQuery(document).ready(function() {';
		$html .= $this->_form['module'].'From.init();';
		$html .= '});';
		$html .= '</script>';
		
		return $html;
	}
	
	/**
	 * 创建无Tab的表单：
	 * @param string $form
	 */
	public function createFormAll($form){
		$html = '';
		foreach($form['ele'] as $key=>$value){
			$html .= $this->createElement($value);			
		}
		
		return $html;
	}
	
	/**
	 * 创建有Tab的表单：
	 * @param string $form
	 */
	public function createFormTab($form){
		$html = '';
		$html .= '<ul class="nav nav-tabs">';
		foreach($form['tab'] as $k=>$v){
			if(!$k){
				$html .= '<li class="active">';
			}else{
				$html .= '<li>';
			}
			
			$html .= '<a href="#tab_1_'.$k.'" data-toggle="tab">';
			$html .= $v['tab_name'][$this->_lang];
			$html .= '</a>';
			$html .= '</li>';
		}
		$html .= '</ul>';
		$html .= '<div class="tab-content">';
		foreach($form['tab'] as $k=>$v){
			if(!$k){
				$html .= '<div class="tab-pane active" id="tab_1_'.$k.'">';
			}else{
				$html .= '<div class="tab-pane" id="tab_1_'.$k.'">';
			}
			if($v['tab_type']=='reply'){
				$html .= $this->tabReply();
			}else{
				foreach($form['ele'] as $key=>$value){
					if($v['tab_id']==$value['file_base']['tab_id']){
						$html .= $this->createElement($value);
					}			
				}
			}
			$html .= '</div>';
		}
		$html .= '</div>';
		
		return $html;
	}
	
	/**
	 * 创造单个表单元素：
	 * @param string $formElement
	 */
	public function createElement($ele_arr){		
		$html = '';
		$html .= '<div class="control-group">';
        $html .= '<label class="control-label">';
		$html .= $ele_arr['file_base']['filed_title'][$this->_lang];
		if(@$ele_arr['valid_attr']['valid']['required']==1){
			$html .= '<span class="required">*</span>';
		}
		$html .= '</label>';
        $html .= '<div class="controls">';
		if(@$ele_arr['filed_show']=='3'){
			$html .= $ele_arr['filed_custom'];
		}else{
			if(method_exists($this,$ele_arr['file_base']['filed_type'])){
				$html .= $this->$ele_arr['file_base']['filed_type']($ele_arr);
			}
		}
        $html .= '</div>';
        $html .= '</div>';		
		
		return $html;
	}
	
	/**
	 * 创造纯净的单个表单元素，不包括表单元素外的其他HTML代码：
	 * @param string $formElement
	 */
	public function createPureElement($ele_arr){		
		$html = '';
		
		if(method_exists($this,$ele_arr['file_base']['filed_type'])){
			$html .= $this->$ele_arr['file_base']['filed_type']($ele_arr);
		}		
		
		return $html;
	}
	
	//后台表单多站点选项支持
	function site($arg){
		$html = '';		
		echo $html;		
	}
	
	//后台表单多终端选项支持
	public function device($ele_arr){
		$html = '';
		
		if(count(C('MOREDEVICE_LIST'))>1){
			$html .= '<div class="control-group">';
			$html .= '<label class="control-label">'.L('cfg_form_ele_device').' <span class="required">*</span></label>';
			$html .= '<div class="controls">';
			foreach(C('MOREDEVICE_LIST') as $k=>$v){
				$checked = '';
				if(strpos('|'.$ele_arr['filed_attr']['default'],$k)){
					$checked = 'checked="checked"';
				}else{
					if($v[1]=='checked'){
						$checked = 'checked="checked"';
					}	
				}
				$html .= '<input '.$checked.' type="checkbox" name="device[]" value="'.$k.'"/> '.L($v[0]).'　　';
			}
			$html .= '</div>';
			$html .= '</div>';
		}else{
			foreach(C('MOREDEVICE_LIST') as $k=>$v){
				$html .= '<input type="hidden" value="'.$k.'" name="device">';
			}
		}
		
		return $html;		
	}
	
	/**
	 * 创造表单元素：cate
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function cate($ele_arr){
		$html = '';
		$html .= '<select class="span6 m-wrap" name="'.$ele_arr['file_base']['filed_name'].'">';
		$html .= '<option value="">'.L('cfg_form_tis_select').'</option>';
		$html .= '</select>';
		
		$html .= '<script type="text/javascript">';
		$html .= 'function get_select(){';
		$html .= 'var module = "'.MODULE_NAME.'";';
		$html .= 'var cateid  = "'.$ele_arr['filed_attr']['default'].'";';
		$html .= 'var lang   = $("select[name=lang]").val();';
		$html .= '$.get("'.U("/Public/ajax").'", {"lang":lang,"module":module,cateid:cateid}, function(data){';
		$html .= 'var option = "<option value=\'0\' m=\''.MODULE_NAME.'\'>'.L('cfg_form_tis_select').'</option>";';
		$html .= '$("select[name=cateid]").html(option+data);	';
				
		$html .= '$("select[name=cateid] option").each(function(){';
		$html .= 'if(module!=$(this).attr("m")){';
		$html .= '$(this).remove();';
		$html .= '}';
		$html .= '});';
		$html .= '});';
		$html .= '}';
		$html .= 'get_select();';
		$html .= '</script>';
		
		return $html;
	}
	
	/**
	 * 创造表单元素：lang
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function lang($ele_arr){
		$html = '';
		$html .= '<select class="span6 m-wrap" name="'.$ele_arr['file_base']['filed_name'].'">';
		$html .= '<option value="">'.L('cfg_form_tis_select').'</option>';
		$html .= create_option_key(C("LANG_ARRAY"),trim($ele_arr['filed_attr']['default']));
		$html .= '</select>';
		
		$html .= '<script type="text/javascript">';
		$html .= 'if(typeof(get_select) != "undefined"){';
		$html .= '$("select[name=lang]").bind("change",function(){';		
		$html .= 'get_select();';		
		$html .= '});';
		$html .= '}';
		$html .= '</script>';
		return $html;
	}
	
	/**
	 * 创造表单元素：text
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function text($ele_arr){
		$html = '';
		$html .= '<input type="text" ';
		$html .= $this -> _inputattr($ele_arr);
		$html .= ' />';
		
		return $html;
	}
	
	/**
	 * 创造表单元素：password
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function password($ele_arr){
		$html = '';
		$html .= '<input type="password" ';
		$html .= $this -> _inputattr($ele_arr);
		$html .= ' />';
		
		return $html;
	}
	
	/**
	 * 创造表单元素：int
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function int($ele_arr){
		$html = '';
		$html .= '<input type="text" ';
		$html .= $this -> _inputattr($ele_arr);
		$html .= ' />';
		
		return $html;
	}
	
	/**
	 * 创造表单元素：float
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function float($ele_arr){
		$html = '';
		$html .= '<input type="text" ';
		$html .= $this -> _inputattr($ele_arr);
		$html .= ' />';
		
		return $html;
	}
	
	/**
	 * 创造表单元素：verify
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function verify($ele_arr){
		$html = '';
		$html .= '<input type="text" ';
		$html .= $this -> _inputattr($ele_arr);
		$html .= ' />';
		$html .= '<img src="'.U('Public/verify').'" onClick="$(this).attr(\'src\',\''.U("Public/verify").'\'+Math.random(1));"/>';
		
		return $html;
	}
	
	/**
	 * 创造表单元素：date
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function date($ele_arr){
		$html = '';
		$html .= '<input type="text" ';
		$html .= $this -> _inputattr($ele_arr);
		if(@$ele_arr['valid_attr']['fieldtype']=='datetime'){
			$html .= ' onFocus="WdatePicker({dateFmt:\'yyyy-M-d H:mm:ss\'})" ';
		}else{
			$html .= ' onFocus="WdatePicker({dateFmt:\'yyyy-M-d\'})" ';
		}
		$html .= ' />';
		
		return $html;
	}
	
	/**
	 * 创造表单元素：textarea
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function textarea($ele_arr){
		$html = '';
		$html .= '<textarea name="'.$ele_arr['file_base']['filed_name'].'" style="width:60%; height:120px;"';
		
		if($ele_arr['valid_attr']['tis']['default']==''){
			$html .= ' placeholder="" ';
		}else{
			$html .= ' placeholder="'.$ele_arr['valid_attr']['tis']['default'][$this->_lang].'" ';
		}
		/*if($ele_arr['valid_attr']['valid']['minlength']!=''){
			$html .= ' minlength="'.$ele_arr['valid_attr']['valid']['minlength'].'" ';
		}
		if($ele_arr['valid_attr']['valid']['maxlength']!=''){
			$html .= ' maxlength="'.$ele_arr['valid_attr']['valid']['maxlength'].'" ';
		}
		if($ele_arr['valid_attr']['valid']['required']==1){
			$html .= ' required="1" ';
		}
		if($ele_arr['valid_attr']['valid']['regex']==1){
			$html .= ' regex="'.$ele_arr['valid_attr']['valid']['regex'].'" ';
		}*/
		
		$html .= '>'.trim($ele_arr['filed_attr']['default']).'</textarea>';
		
		return $html;
	}
	
	/**
	 * 创造表单元素：radio
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function radio($ele_arr){
		$html = '';
		$html .= '<div class="input_radio_box"';
		
		/*if($ele_arr['valid_attr']['valid']['required']==1){
			$html .= ' required="1" ';
		}*/
		
		$options_id = $ele_arr['filed_attr']['options'];
		$Model_dict = D("Dict");
		$data_dict = $Model_dict->find($options_id);
		$options = F('Dict_'.$data_dict['key']);
			
		if(is_array($options)){
			foreach($options as $k=>$v){
				$value = json_decode($v['value'],true);
				$html .= '<label class="input_checkbox_label"><input type="radio" class="input_checkbox name="'.$ele_arr['file_base']['filed_name'].'" '.$ele_arr['file_base']['css_class'].'" value="'.$v['id'];
				if(trim($ele_arr['filed_attr']['default'])==$v['id']){
					$html .= '  checked="checked"  ';
				}
				$html .= '"/>'.$value[$this->_lang].'</label> ';
			}
		}
		
		return $html;
	}
	
	/**
	 * 创造表单元素：checkbox
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function checkbox($ele_arr){
		$html = '';
		$html .= '<div class="input_checkbox_box"';

		/*if($ele_arr['valid_attr']['valid']['minlength']!=''){
			$html .= ' minlength="'.$ele_arr['valid_attr']['valid']['minlength'].'" ';
		}
		if($ele_arr['valid_attr']['valid']['maxlength']!=''){
			$html .= ' maxlength="'.$ele_arr['valid_attr']['valid']['maxlength'].'" ';
		}
		if($ele_arr['valid_attr']['valid']['required']==1){
			$html .= ' required="1" ';
		}*/
		
		$options_id = $ele_arr['filed_attr']['options'];
		$Model_dict = D("Dict");
		$data_dict = $Model_dict->find($options_id);
		$options = F('Dict_'.$data_dict['key']);
			
		if(is_array($options)){
			foreach($options as $k=>$v){
				$value = json_decode($v['value'],true);
				$html .= '<label class="input_checkbox_label"><input type="checkbox" class="input_checkbox name="'.$ele_arr['file_base']['filed_name'].'[]" '.$ele_arr['file_base']['css_class'].'" value="'.$v['id'];
				if(trim($ele_arr['filed_attr']['default'])==$v['id']){
					$html .= '  checked="checked"  ';
				}
				$html .= '"/>'.$value[$this->_lang].'</label> ';
			}
		}
		
		$html .= '</div>';
		
		return $html;
	}
	
	/**
	 * 创造表单元素：select
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function select($ele_arr){		
		$html = '';
		$html .= ' <select class="span6 m-wrap '.$ele_arr['file_base']['css_class'].'" name="'.$ele_arr['file_base']['filed_name'].'">';
		$html .= '<option value="">'.L('cfg_form_tis_select').'</option>';
		
		$options_id = $ele_arr['filed_attr']['options'];
		$Model_dict = D("Dict");
		$data_dict = $Model_dict->find($options_id);
		$options = F('Dict_'.$data_dict['key']);
			
		if(is_array($options)){
			foreach($options as $k=>$v){
				$html .= '<option value="'.$v['id'].'"';
				if(trim($ele_arr['filed_attr']['default'])==$v['id']){
					$html .= ' selected ';
				}
				
				$value = json_decode($v['value'],true);
				$html .= '>'.$value[$this->_lang].'</option>';
			}
		}
		
		$html .= '</select>';
		
		return $html;
	}
	
	/**
	 * 创造表单元素：linkage
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function linkage($ele_arr){
	
		$options_id = $ele_arr['filed_attr']['options'];
		$Model_dict = D("Dict");
		$data_dict = $Model_dict->find($options_id);
		$options = F('Dict_'.$data_dict['key']);
		$key = $data_dict['key'];
		
		//查找当前选中的Level
		$data_default = $Model_dict->find($ele_arr['filed_attr']['default']);
		if($data_default['level']){
			$default_level = $data_default['level'] ;
		}else{
			$default_level = 1 ;
			$ele_arr['filed_attr']['default'] = '';
		}
		
		if(is_array($options)){
			foreach($options as $k=>$v){
				if($v['pid']){
					$value = json_decode($v['value'],true);
					$jsjson_arr[$v['id']] = array($value[$this->_lang],$v['pid'],$v['level']);
				}else{
					$frist_id = $v['id'];
				}
			}
		}
		
		$cfg_form_tis_select=L('cfg_form_tis_select');
		$html = '';
		$html .= '<script type="text/javascript">';
		$html .= 'var linkage_json'.$key.' = '.json_encode($jsjson_arr).';';
$html .= <<< HTML
\$(function(){
	//初始值处理
	var did = '{$ele_arr['filed_attr']['default']}';
	if(did!=''){
		\$('#{$ele_arr['file_base']['filed_name']}Box').html(dealInitial{$ele_arr['file_base']['filed_name']}(did,'',{$default_level}));
	}else{
		\$('#{$ele_arr['file_base']['filed_name']}Box').html(showLinkage{$ele_arr['file_base']['filed_name']}({$frist_id},2,0));
	}
	\$('select[name="{$ele_arr['file_base']['filed_name']}"]').live('change',function(){
		var nowLevel = parseInt(\$(this).attr('level'));
		var nextLevel = nowLevel+1;
		//清除自此以后的select
		\$('select[name="{$ele_arr['file_base']['filed_name']}"]').each(function(index, element) {
			if(parseInt(\$(this).attr('level'))>nowLevel){
				\$(this).remove();
			}
		});
		
		//显示下一个select
		\$('#{$ele_arr['file_base']['filed_name']}Box').append(showLinkage{$ele_arr['file_base']['filed_name']}(\$(this).val(),nextLevel,0));
	});
	
	//处理初始值
	function dealInitial{$ele_arr['file_base']['filed_name']}(lid,result,level){
		if(lid){
			$.each(linkage_json{$key} , function(i, n){
				if(lid==i){
					result = dealInitial{$ele_arr['file_base']['filed_name']}(n[1],result,(parseInt(level)-1))+''+showLinkage{$ele_arr['file_base']['filed_name']}(n[1],level,i);
					return false;
				}
			});
		}
		
		return result;
	}
	
	//响应select改变事件
	function showLinkage{$ele_arr['file_base']['filed_name']}(pid,level,nid){
		var html = ''; //最终输出的HTML
		var have = false; //是否有子词典
		html += '<select name="{$ele_arr['file_base']['filed_name']}" id="{$ele_arr['file_base']['filed_name']}'+level+'" level="'+level+'">';
		html += '<option value="0">{$cfg_form_tis_select}</option>';
		\$.each(linkage_json{$key} , function(i, n){
			if(pid==n[1]){
				if(nid==i){
					html += '<option value="'+i+'" selected>'+n[0]+'</option>';
				}else{
					html += '<option value="'+i+'">'+n[0]+'</option>';
				}
				have = true;
			}
		});
		html += '</select>';
		
		//没有子词典则此次输出为空
		if(!have){
			html = '';
		}
		
		return html;
	}
});
HTML;
		$html .= '</script>';
		$html .= '<div id="'.$ele_arr['file_base']['filed_name'].'Box"></div>';
		
		return $html;
	}

	/**
	 * 创造表单元素：signle
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function signle($ele_arr){
		$html = '';
		$html .= '<a class="btn blue" id="'.$ele_arr['file_base']['filed_name'].'_uploadPicBtn" data-toggle="modal"><i class="icon-picture"></i> '.L('cfg_upl_thumbpic_btn').'</a>';
		$html .= '<div class="clear" style="height:20px;"></div>';
		$html .= '<div class="alert">';
		$html .= '<button data-dismiss="alert" class="close"></button>';
		$html .= L('cfg_upl_thumbpic_wx_tis');
		$html .= '</div>';
		$html .= '<div class="clear"></div>';
		$html .= '<div id="'.$ele_arr['file_base']['filed_name'].'_uploadPicBox" class="uploadPic_box">';
		if($ele_arr['filed_attr']['default']){
			$html .= $ele_arr['filed_attr']['default'];
		}
		$html .= '</div>';
		$html .= '<script type="text/javascript">';
		$html .= "uploadPic.init([
				{
					'btn':'".$ele_arr['file_base']['filed_name']."_uploadPicBtn',
					'title':jsLang.uploadSinglePic,
					'box':'".$ele_arr['file_base']['filed_name']."_uploadPicBox',
					'ptype':'single',
					'field':'".$ele_arr['file_base']['filed_name']."'
				}],jsLang);";
        $html .= '</script>';
		
		return $html;
	}
	/**
	 * 创造表单元素：atlas
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function atlas($ele_arr){
		$html = '';
		$html .= '<a class="btn blue" id="'.$ele_arr['file_base']['filed_name'].'_uploadPicBtn" data-toggle="modal"><i class="icon-picture"></i> '.L('cfg_upl_atlaspic_btn').'</a>';
		$html .= '<div class="clear"></div>';
		$html .= '<div id="'.$ele_arr['file_base']['filed_name'].'_uploadPicBox" class="uploadPic_box">';
		if($ele_arr['filed_attr']['default']){
			$html .= $ele_arr['filed_attr']['default'];
		}
		$html .= '</div>';
		$html .= '<script type="text/javascript">';
		$html .= "uploadPic.init([
				{
					'btn':'".$ele_arr['file_base']['filed_name']."_uploadPicBtn',
					'title':jsLang.uploadSinglePic,
					'box':'".$ele_arr['file_base']['filed_name']."_uploadPicBox',
					'ptype':'atlas',
					'field':'".$ele_arr['file_base']['filed_name']."'
				}],jsLang);";
        $html .= '</script>';
		
		return $html;
	}
	/**
	 * 创造表单元素：file
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function file($ele_arr){
		$html = '';
		$html .= '<a class="btn blue" id="'.$ele_arr['file_base']['filed_name'].'_uploadFileBtn" data-toggle="modal"><i class="icon-picture"></i> '.L('cfg_upl_files_btn').'</a>';
		$html .= '<div class="clear"></div>';
		$html .= '<div id="'.$ele_arr['file_base']['filed_name'].'_uploadFileBox" class="uploadPic_box">';
		if($ele_arr['filed_attr']['default']){
			$html .= $ele_arr['filed_attr']['default'];
		}
		$html .= '</div>';
		$html .= '<script type="text/javascript">';
		$html .= "uploadFile.init([
				{
					'btn':'".$ele_arr['file_base']['filed_name']."_uploadFileBtn',
					'title':jsLang.uploadFiles,
					'box':'".$ele_arr['file_base']['filed_name']."_uploadFileBox',
					'ptype':'file',
					'field':'".$ele_arr['file_base']['filed_name']."'
				}],jsLang);";
        $html .= '</script>';
		
		return $html;
	}
	/**
	 * 创造表单元素：files
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function files($ele_arr){
		$html = '';
		$html .= '<a class="btn blue" id="'.$ele_arr['file_base']['filed_name'].'_uploadFileBtn" data-toggle="modal"><i class="icon-picture"></i> '.L('cfg_upl_files_btn').'</a>';
		$html .= '<div class="clear"></div>';
		$html .= '<div id="'.$ele_arr['file_base']['filed_name'].'_uploadFileBox" class="uploadPic_box">';
		if($ele_arr['filed_attr']['default']){
			$html .= $ele_arr['filed_attr']['default'];
		}
		$html .= '</div>';
		$html .= '<script type="text/javascript">';
		$html .= "uploadFile.init([
				{
					'btn':'".$ele_arr['file_base']['filed_name']."_uploadFileBtn',
					'title':jsLang.uploadFiles,
					'box':'".$ele_arr['file_base']['filed_name']."_uploadFileBox',
					'ptype':'file',
					'field':'".$ele_arr['file_base']['filed_name']."'
				}],jsLang);";
        $html .= '</script>';
		
		return $html;
	}
	/**
	 * 创造表单元素：htmlarea
	 * @param string $ele_arr 表单元素属性数组
	 */
	public function htmlarea($ele_arr){
		$html = '';
		$html .= ' <textarea name="'.$ele_arr['file_base']['filed_name'].'" style="width:100%; height:400px;">'.$ele_arr['filed_attr']['default'].'</textarea>';
		$html .= '<script type="text/javascript">';
		$html .= '$(\'textarea[name="'.$ele_arr['file_base']['filed_name'].'"]\').xheditor({modalWidth:650,modalHeight:400,upBtnText:jsLang.uploadScan,upLinkUrl:"!'.TMPL_URL.C('DEFAULT_THEME').'/Assets/plugins/ckfinder/upload.html?type=File",upImgUrl:"!'.TMPL_URL.C('DEFAULT_THEME').'/Assets/plugins/ckfinder/upload.html?type=Photo",upFlashUrl:"!'.TMPL_URL.C('DEFAULT_THEME').'/Assets/plugins/ckfinder/upload.html?type=Flash",upMediaUrl:"!'.TMPL_URL.C('DEFAULT_THEME').'/Assets/plugins/ckfinder/upload.html?type=Media"});';
        $html .= '</script>';
		
		return $html;
	}
	
	/**
	 * 回复Tab
	 */
	private function tabReply(){
		$html = '';
		$html .= '<div class="row-fluid">';
		$html .= '<div class="span6">';
		$html .= '<button type="button" id="btn_addReply" class="btn blue">';
		$html .= '<i class="icon-plus"></i>';
		$html .= L('cfg_form_wechat_reply_add');
		$html .= '</button>';
		$html .= '<button type="button" id="btn_refresh" class="btn green  icn-only">';
		$html .= '<i class="icon-refresh"></i>';
		$html .= '</button>';
		$html .= '</div>';
		$html .= '</div>';
		$html .= '<div class="clear" style="height:10px;"></div>';
		$html .= '<table class="table table-striped table-bordered table-hover table-condensed flip-content">';
		$html .= '<thead>';
		$html .= '<tr class="list_head ct" style="text-align:center">';
		$html .= '<td>'.L('cfg_form_reply_type').'</td>';
		$html .= '<td>'.L('cfg_form_wechat_reply_type').'</td>';
		$html .= '<td >'.L('cfg_form_wechat_reply_explain').'</td>';
		$html .= '<td>'.L('cfg_form_ele_opreate').'</td>';
		$html .= '</tr>';
		$html .= '</thead>';
		$html .= '<tbody id="tableList">';
		$html .= '</tbody>';
		$html .= '</table>';
		
		$html .= '<script src="'.TMPL_URL.C('DEFAULT_THEME').'/Assets/scripts/wechat/list.js"></script>';
		$html .= '<script type="text/javascript">';
		$html .= 'jQuery(document).ready(function() {';
		//新增回复
		$html .= '$(\'#btn_addReply\').on(\'click\',function(){';
		$html .= 'var table   = "Article";';
		$html .= 'var solevar = $("input[name=solevar]").val();';
		$html .= 'art.dialog.open(\''.U("/Reply/content_add").'\'+\'&solevar=\'+solevar+\'&table=\'+table,{title: "'.L('cfg_form_wechat_reply_add').'", width:600,height:420,fixed:true,top:60});';
		$html .= '});';
		//刷新回复
		$html .= '$(\'#btn_refresh\').on(\'click\',function(){';
		$html .= 'getListReply();';
		$html .= '});';
		//删除回复
		$html .= '$(\'.delAlone\').live(\'click\',function(){';
		$html .= '$.get(\''.U("/Reply/delcont").'\'+\'&id=\'+$(this).attr(\'data-id\'));';
		$html .= 'getListReply();';
		$html .= '});';
		//获取类型列表
		$html .= 'get_select();';
		$html .= '$(\'select[name=lang]\').bind(\'change\',function(){';
		$html .= 'get_select();';
		$html .= '});';
		$html .= 'getListReply();';
		$html .= '});';
		
		//获取回复列表
		$html .= 'function getListReply(){';
		$html .= '$(\'#tableList\').html(\'<tr><td colspan="3"><div id="ajaxLoadingIco" style="text-align:center;padding:20px;">\'+\'<img src="\'+ABS_URL_Theme+\'/Assets/img/ajax-loading.gif"/></div></td></tr>\');';
		$html .= 'var solevar = $(\'input[name=solevar]\').val();';
		$html .= 'var parameter = {};';
		$html .= 'parameter.url = \''.U("/Reply/content_reply").'\'+\'&solevar=\'+solevar;';
		$html .= 'parameter.listId = \'tableList\';';
		$html .= 'wechatList.init(parameter);';		
		$html .= '}';
		
		//获取回复类型
		$html .= 'function get_select(){';
		$html .= 'var module = \''.MODULE_NAME.'\';';
		$html .= 'var lang   = $("select[name=lang]").val();';
		$html .= '$.get(\''.U("/Public/ajax").'\', {\'lang\':lang,\'module\':module}, function(data){var option = "<option value=\'0\' m=\''.MODULE_NAME.'\'>{$Think.lang.cfg_form_tis_select}</option>";';
		$html .= '$("select[name=cateid]").html(option+data);';
		$html .= '$("select[name=cateid] option").each(function(){';
		$html .= 'if(module!=$(this).attr("m")){';
		$html .= '$(this).remove();';
		$html .= '}';
		$html .= '});';
		$html .= '});';
		$html .= '}';
		
		$html .= '</script>';
		
		return $html;
	}
	
	/*****************************私有方法开始********************************/
	/**
	* 表单验证
	*/
	private function _validate(){
		$html = '';
		$html .= '<script type="text/javascript">';
		$html .= 'var '.$this->_form['module'].'From = function () {';
		$html .= 'return {';
		$html .= 'init: function () {';
		//新增正则表达式方法
		$html .= '$.validator.addMethod(';
		$html .= '"regex",';
		$html .= 'function(value, element, params) {';
		$html .= 'var reg = new RegExp(params);';
		$html .= 'return reg.test(value);';
		$html .= '},';
		$html .= '"Did not pass the regular verification"';
		$html .= ');';
		
		//获取表单
		$html .= 'var vform = $("#'.$this->_form['module'].'From");';
		$html .= 'var verror = $(".alert-error", vform);';
		$html .= 'var vsuccess = $(".alert-success", vform);';
		$html .= 'vform.validate({';
		$html .= 'errorElement: "span",';
		$html .= 'errorClass: "help-inline",';
		$html .= 'focusInvalid: false,';
		$html .= 'ignore: "",';

		//处理验证规则
		$rules_str = '';
		$rules_str .= '1: 1';
		$messages_str = '';
		$messages_str .= '1: 1';
		foreach($this->_form['ele'] as $ak=>$av){	
			$rules_str .= ','.$av['file_base']['filed_name'].': {';
			$messages_str .= ','.$av['file_base']['filed_name'].': {';
			$rules_str .= '1: 1';
			$messages_str .= '1: 1';
			if($av['valid_attr']['valid']['required'] == '1'){
				$rules_str .= ',required: true';
				if($av['valid_attr']['auto']['required_tis']!='ok'){
					$messages_str .= ',required: "'.$av['valid_attr']['tis']['required_'.$this->_lang].'"';
				}else{
					$messages_str .= ',required: "'.L('cfg_form_tipmsg_empty').'"';
				}
			}
			if($av['valid_attr']['valid']['minlength'] != ''){
				$rules_str .= ',minlength: '.$av['valid_attr']['valid']['minlength'].'';
				if($av['valid_attr']['auto']['minlength_tis']!='ok'){
					$messages_str .= ',minlength: "'.$av['valid_attr']['tis']['minlength_'.$this->_lang].'"';
				}else{
					$messages_str .= ',minlength: "'.L('cfg_form_tipmsg_minlength').'"';
				}
			}
			if($av['valid_attr']['valid']['maxlength'] != ''){
				$rules_str .= ',maxlength: '.$av['valid_attr']['valid']['maxlength'].'';
				if($av['valid_attr']['auto']['maxlength_tis']!='ok'){
					$messages_str .= ',maxlength: "'.$av['valid_attr']['tis']['maxlength_'.$this->_lang].'"';
				}else{
					$messages_str .= ',maxlength: "'.L('cfg_form_tipmsg_maxlength').'"';
				}
			}
			if($av['valid_attr']['valid']['regex'] != ''){
				$rules_str .= ',regex: "'.$av['valid_attr']['valid']['regex'].'"';
				if($av['valid_attr']['auto']['regex_tis']!='ok'){
					$messages_str .= ',regex: "'.$av['valid_attr']['tis']['regex_'.$this->_lang].'"';
				}else{
					$messages_str .= ',regex: "'.L('cfg_form_tipmsg_regex').'"';
				}
			}
			$rules_str .= '}';
			$messages_str .= '}';
		}
		//替换无用字符1: 1,
		$rules_str = str_replace("1: 1,", "", $rules_str);
		$messages_str = str_replace("1: 1,", "", $messages_str);
		
		//验证规则
		$html .= 'rules: {';
		$html .= $rules_str;
        $html .= '},';
		//验证提示
		$html .= 'messages: {';
		$html .= $messages_str;
        $html .= '},';
		
		$html .= 'invalidHandler: function (event, validator) {';
		$html .= 'vsuccess.hide();';
		$html .= 'verror.show();';
		$html .= 'App.scrollTo(verror, -200);';
		$html .= '},';
		$html .= 'highlight: function (element) {';
		$html .= '$(element).closest(".help-inline").removeClass("ok");';
		$html .= '$(element).closest(".control-group").removeClass("success").addClass("error");';
		$html .= '},';
		$html .= 'unhighlight: function (element) {';
		$html .= '$(element).closest(".control-group").removeClass("error");';
		$html .= '}';
		$html .= '});';
		$html .= '}';
		$html .= '}';
		$html .= '}();';
		$html .= '</script>';
		
		return $html;
	}
	
	/**
	 * 包含所需的JS
	 */
	private function _linkJS(){
		$html = '';
		$has_htmlarea = false;
		$has_pic = false;
		$has_file = false;
		
		$html .= '<script type="text/javascript">';
		$html .= 'var jsLang = {};';
		$html .= '</script>';
		
		foreach($this->_form['ele'] as $v){
			if($v['file_base']['filed_type']=='htmlarea'){
				if(!$has_htmlarea){
					$has_htmlarea = true;
					$html .= '<script type="text/javascript" src="'.TMPL_URL.C('DEFAULT_THEME').'/Assets/plugins/xheditor/xheditor-1.2.1.min.js"></script>';
					$html .= '<script type="text/javascript" src="'.TMPL_URL.C('DEFAULT_THEME').'/Assets/plugins/xheditor/xheditor_lang/'.$this->_lang.'.js"></script>';
				}
			}elseif(!$has_pic && $v['file_base']['filed_type']=='signle' || $v['file_base']['filed_type']=='atlas'){
				if(!$has_pic){
					$has_pic = true;
					$html .= '<script src="'.TMPL_URL.C('DEFAULT_THEME').'/Assets/scripts/upload/pic.js"></script>';
					$html .= '<script type="text/javascript">';
					$html .= 'jsLang.crop_title = "'.L("cfg_upl_crop_title").'";';
					$html .= 'jsLang.crop_false = "'.L("cfg_upl_crop_false").'";';
					$html .= 'jsLang.crop_noselect = "'.L("cfg_upl_crop_noselect").'";';
					$html .= 'jsLang.uploadSinglePic = "'.L("cfg_upl_thumbpic_btn").'";';
					$html .= 'jsLang.uploadAtlasPic = "'.L("cfg_upl_atlaspic_btn").'";';
					$html .= 'jsLang.uploadScan = "'.L("cfg_upl_scan").'";';
					$html .= '</script>';
				}
			}elseif(!$has_file && $v['file_base']['filed_type']=='file' || $v['file_base']['filed_type']=='files'){
				if(!$has_file){
					$has_file = true;
					$html .= '<script src="'.TMPL_URL.C('DEFAULT_THEME').'/Assets/scripts/upload/file.js"></script>';
					$html .= '<script type="text/javascript">';
					$html .= 'jsLang.uploadFiles = "'.L("cfg_upl_scan").'";';
					$html .= '</script>';
				}
			}elseif($v['file_base']['filed_type']=='date'){
				$html .= '<script src="'.TMPL_URL.C('DEFAULT_THEME').'/Assets/plugins/datepicker/WdatePicker.js"></script>';
			}
			
		}		
		
		return $html;
	}
	/**
	 * 常用正则表达式：_attr
	 * @param string $ele_arr 表单元素属性数组
	 */
	private function _inputattr($ele_arr){
		$html = '';
		
		$html .= ' name="'.$ele_arr['file_base']['filed_name'].'" '; //表单name属性
		
		//验证：CSS
		if(trim($ele_arr['file_base']['css_class'])){
			$html .= ' class="span6 m-wrap '.$ele_arr['file_base']['css_class'].'" ';
		}else{
			$html .= ' class="span6 m-wrap" ';
		}
		//提示：默认提示
		if($ele_arr['valid_attr']['tis']['default']==''){
			$html .= ' placeholder="" ';
		}else{
			$html .= ' placeholder="'.$ele_arr['valid_attr']['tis']['default'][$this->_lang].'" ';
		}
		/*//验证：最小长度
		if($ele_arr['valid_attr']['valid']['minlength']!=''){
			$html .= ' minlength="'.$ele_arr['valid_attr']['valid']['minlength'].'" ';
		}
		//验证：最大长度
		if($ele_arr['valid_attr']['valid']['maxlength']!=''){
			$html .= ' maxlength="'.$ele_arr['valid_attr']['valid']['maxlength'].'" ';
		}
		//验证：必填
		if($ele_arr['valid_attr']['valid']['required']==1){
			$html .= ' required="1" ';
		}
		//验证：正则
		if($ele_arr['valid_attr']['valid']['regex']==1){
			$html .= ' regex="'.$ele_arr['valid_attr']['valid']['regex'].'" ';
		}*/
		
		//默认值
		if(trim($ele_arr['filed_attr']['default'])==''){
			$html .= ' value="" ';
		}else{
			$html .= ' value="'.trim($ele_arr['filed_attr']['default']).'" ';
		}
		
		return $html;
	}
	
	/**
	 * 常用正则表达式：_regex
	 * @param string $type 正则类型
	 */
	private function _regex($type){
		$html = '';
		if($type=='email'){
			//邮箱号码
			$html .= '/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/';
		}elseif($type=='ip'){
			//IP地址
			$html .= '/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g';
		}elseif($type=='phone'){
			//电话号码
			$html .= '/^[0][1-9]{2,3}-[0-9]{5,10}$/';
		}elseif($type=='cn_mobile'){
			//手机号码，包括联通、电信、移动
			$html .= '/^(1(3[4-9]|5[012789]|8[78])\d{8})|(18[09]\d{8})|(1(3[0-2]|5[56]|8[56])\d{8})|(1[35]3\d{8})$/';
		}elseif($type=='cn_mobile_yd'){
			//手机号码，中国移动
			$html .= '/^1(3[4-9]|5[012789]|8[78])\d{8}$/';
		}elseif($type=='cn_mobile_dx'){
			//手机号码，中国电信
			$html .= '/^18[09]\d{8}$/';
		}elseif($type=='cn_mobile_lt'){
			//手机号码，中国联通
			$html .= '/^1(3[0-2]|5[56]|8[56]$/';
		}elseif($type=='int'){
			//整数
			$html .= '/^[-]{0,1}[0-9]{1,}$/';
		}elseif($type=='pint'){
			//正整数
			$html .= '/^[0-9]+$/';
		}elseif($type=='float'){
			//浮点数
			$html .= '/^[-]{0,1}(\d+)[\.]+(\d+)$/';
		}elseif($type=='pfloat'){
			//正浮点数
			$html .= '/^(\d+)[\.]+(\d+)$/';
		}elseif($type=='money'){
			//检查输入字符串是否符合金额格式,格式定义为带小数的正数，小数点后最多三位
			$html .= '/^[0-9]+[\.][0-9]{0,3}$/';
		}elseif($type=='letter'){
			//判断是否是数字或字母
			$html .= '/^[0-9a-zA-Z\_]+$/';
		}elseif($type=='cletter'){
			//判断是否是汉字、字母、数字组成 
			$html .= '/^[0-9a-zA-Z\u4e00-\u9fa5]+$/';
		}elseif($type=='date'){
			$html .= '/^\d{4}-\d{2}-\d{2}$/';
		}elseif($type=='datetime'){
			$html .= '/^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/';
		}elseif($type=='chinese'){
			$html .= '/[^\u4e00-\u9fa5]/gi';
		}
		
		return $html;
	}
	/*****************************私有方法结束********************************/
}
